<?php
require_once 'phpmoko/lib/common.php';

require_once 'phpmoko/externals/AjaxUpload.php';
require_once 'phpmoko/ui/RootPanel.php';
require_once 'phpmoko/ui/Widget.php';

class AsynchronousFileUpload extends Widget
{
	/**
	 *
	 * @var ListPanel
	 */
	private $list = null;
	private $handler = null;
	private $script = null;
	private $textButton = "Upload a file";
	private $textDropFiles = "Drop files here to upload";
	private $textCancel = "Cancel";
	private $textFailed = "Failed";

	function __construct()
	{
		parent::__construct();

		AjaxUpload::activate();
	}

	function setUploadScript($script)
	{
		$this->script = $script;
	}

	function setWidgetHandler($handler)
	{
		$this->handler = $handler;
	}

	function setTextButton($text)
	{
		$this->textButton = $text;
	}

	function setTextDropFiles($text)
	{
		$this->textDropFiles = $text;
	}

	function setTextCancel($text)
	{
		$this->textCancel = $text;
	}

	function setTextFailed($text)
	{
		$this->textFailed = $text;
	}

	/**
	 *
	 * @param ListPanel $list
	 */
	function setListPanel($list)
	{
		$this->list = $list;
	}

	function toString()
	{
		$code = "var uploader = new qq.FileUploader({";

		$code .= "template: '<div class=\"qq-uploader\">";
		$code .= "<div class=\"qq-upload-drop-area\"><span>" . $this->textDropFiles . "</span></div>";
		$code .= "<div class=\"qq-upload-button\">" . $this->textButton . "</div>";
		$code .= "<ul class=\"qq-upload-list\"></ul>";
		$code .= "</div>',";

		$code .= "fileTemplate: '<li>";
		$code .= "<span class=\"qq-upload-file\"></span>";
		$code .= "<span class=\"qq-upload-spinner\"></span>";
		$code .= "<span class=\"qq-upload-size\"></span>";
		$code .= "<a class=\"qq-upload-cancel\" href=\"#\">" . $this->textCancel . "</a>";
		$code .= "<span class=\"qq-upload-failed-text\">" . $this->textFailed . "</span>";
		$code .= "</li>',";

		$code .= "element: document.getElementById('" . $this->handler->getId() . "'),";

		if ($this->list != null)
			$code .= "listElement: document.getElementById('" . $this->list->getId() . "'),";

		$code .= "action: '" . $this->script . "'";
		$code .= "});";

		$root = RootPanel::get();
		$root->addJSOnReadyDocument($code);

		return '';
	}
}
?>